package com.zsk.parse.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 写excel表格 https://blog.csdn.net/qq_34474324/article/details/97627474
 * 
 * @author zincredible
 * @date 2020/03/11 11:15:02
 */
// <dependency>
// <groupId>org.apache.poi</groupId>
// <artifactId>poi-ooxml</artifactId>
// <version>3.17</version>
// </dependency>
public class WriteExcel {

    public final static String XLS = ".xls";
    public final static String XLSX = ".xlsx";

    /**
     * 创建xls表格
     * 
     * @param title
     * @param content
     * @param filePath
     * @date 2020/03/11 14:02:58
     * @author zincredible
     */
    public static void writeXls(String[] title, List<ArrayList<String>> content, String filePath) {
        // 创建Excel文件薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表sheeet
        HSSFSheet sheet = workbook.createSheet();
        // 创建第一行标题
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        int rowNum = content.size();
        // 追加数据
        for (int i = 1; i < rowNum; i++) {
            List<String> coList = content.get(i);
            HSSFRow nextrow = sheet.createRow(i);
            int colNum = coList.size();
            for (int j = 0; j < colNum; j++) {
                cell = nextrow.createCell(j);
                cell.setCellValue(coList.get(j));
            }
        }
        // 创建一个文件
        File file = new File(filePath);
        FileOutputStream fo = null;
        try {
            file.createNewFile();
            fo = new FileOutputStream(file);
            workbook.write(fo);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (fo != null)
                    fo.close();
                if (workbook != null)
                    workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建xlsx表格
     * 
     * @param title
     * @param content
     * @param filePath
     * @date 2020/03/11 14:03:46
     * @author zincredible
     */
    public static void writeXlsx(String[] title, List<ArrayList<String>> content, String filePath) {
        // 创建Excel文件薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 创建工作表sheeet
        Sheet sheet = workbook.createSheet();
        // 创建第一行
        Row row = sheet.createRow(0);
        Cell cell = null;
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        int rowNum = content.size();
        // 追加数据
        for (int i = 1; i < rowNum; i++) {
            List<String> coList = content.get(i);
            Row nextrow = sheet.createRow(i);
            int colNum = coList.size();
            for (int j = 0; j < colNum; j++) {
                cell = nextrow.createCell(j);
                cell.setCellValue(coList.get(j));
            }
        }
        // 创建一个文件
        File file = new File(filePath);
        FileOutputStream fo = null;
        try {
            file.createNewFile();
            fo = new FileOutputStream(file);
            workbook.write(fo);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fo != null)
                    fo.close();
                if (workbook != null)
                    workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String[] title = new String[] {"测试1", "测试2", "测试3", "测试4"};
        List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
        for (int i = 0; i < 10; i++) {
            ArrayList<String> row = new ArrayList<>();
            for (int j = 0; j < title.length; j++) {
                row.add(UUID.randomUUID().toString().substring(0, 7) + j);
            }
            list.add(row);
        }
        writeXls(title, list, "/poi-xls-test.xls");
        writeXlsx(title, list, "/poi-xls-test.xlsx");
    }

}
